package cn.cqie.attendance.manager.service.impl;

import cn.cqie.attendance.common.util.Page;
import cn.cqie.attendance.manager.dao.AdminClassesDao;
import cn.cqie.attendance.manager.dao.AdminGroupDao;
import cn.cqie.attendance.manager.pojo.AdminClasses;
import cn.cqie.attendance.manager.pojo.AdminGroup;
import cn.cqie.attendance.manager.pojo.AdminStudent;
import cn.cqie.attendance.manager.service.AdminGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 697
 * @date 2021/6/22 10:46
 */
@Service
public class AdminGroupServiceImpl implements AdminGroupService {


    @Autowired
    private AdminGroupDao adminGroupDao;


    /**
     * 查看小组信息、可以按照小组状态、小组名称查看
     * @param status 小组状态
     * @param groupName 小组名
     * @return
     */
    @Override
    public Page<AdminGroup> queryGroupInfoByStatusAndGroupName(Integer status,String groupName) {
//        System.out.println("----------------------");
//        System.out.println(status);
//        System.out.println(groupName);
//        System.out.println("----------------------");

        AdminGroup adminGroup = new AdminGroup();
        adminGroup.setStatus(status);
        adminGroup.setGroupName(groupName);

//        System.out.println("******************");
//        System.out.println(adminGroup.getGroupName());
//        System.out.println(adminGroup.getStatus());
//        System.out.println("********************");
        List<AdminGroup> list = adminGroupDao.queryGroupInfoByStatusAndGroupName(adminGroup);
        int total = list.size();
        if (null == list || total == 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success", 0, 0, total, list);

    }


    /**
     * 新建分组
     * @param status 状态
     * @param groupName 分组名称
     * @return
     */
    @Override
    public Page addGroup(Integer status,String groupName) {
        //赋值
        AdminGroup adminGroup = new AdminGroup();
        adminGroup.setStatus(status);
        adminGroup.setGroupName(groupName);
        //判断是否重复
        int msg = adminGroupDao.queryCountGroupInfoByGroupName(adminGroup);
        if (msg>0){
            return Page.INFOERROR;
        }
        //继续执行新增
        else{
            int i = adminGroupDao.addGroup(adminGroup);
            if (i>0){
                return Page.SUCCESS;
            }
            else{
                return Page.FAIL;
            }
        }
    }


    /**
     * 根据分组名称查询分组数量
     * @param adminGroup 分组名称
     * @return
     */
    @Override
    public int queryCountGroupInfoByGroupName(AdminGroup adminGroup) {
        int i = adminGroupDao.queryCountGroupInfoByGroupName(adminGroup);
        return i;
    }

    /**
     * 用于判断要修改的小组是否结业
     * @param groupId
     * @return
     */
    @Override
    public int queryGroupStatusFalse(Integer groupId) {
        return 0;
    }


    /**
     * 根据分组ID修改分组信息  名称、状态
     * @param groupName 分组名称
     * @param status 分组状态
     * @param groupId 分组ID
     * @return
     */
    @Override
    public Page updateGroupInfoById(String groupName,Integer status, Integer groupId) {
        //赋值
        AdminGroup adminGroup = new AdminGroup();
        adminGroup.setGroupName(groupName);
        adminGroup.setStatus(status);
        adminGroup.setId(groupId);

        //判读是否重复
        int i = adminGroupDao.queryCountGroupInfoByGroupName(adminGroup);
        int j = adminGroupDao.queryGroupStatusFalse(groupId);//无法修改已经结业的小组 -->status=1
        if (i>1||j==1){
            return Page.INFOERROR;
        }
        //继续执行修改
        else{
            int msg = adminGroupDao.updateGroupInfoById(adminGroup, groupId);
            if (msg>0){
                return Page.SUCCESS;
            }else{
                return Page.FAIL;
            }
        }
    }


    /**
     * 根据分组ID删除分组信息
     * @param groupId 分组ID
     * @return
     */
    @Override
    public Page deleteGroupById(Integer groupId) {
        int i = adminGroupDao.deleteGroupById(groupId);
        if (i>0){
            return Page.SUCCESS;
        }else{
            return Page.INFOERROR;
        }
    }


    /**
     * 根据分组ID查询分组学生信息 模糊 可组合查询
     * @param groupName 分组名称
     * @param stuName 学生姓名
     * @param stuNumber 学号
     * @param groupId 分组ID
     * @return
     */
    @Override
    public Page<AdminStudent> queryStudentByGroupIdCombination(String groupName, String stuName, String stuNumber, Integer groupId) {
        //赋值
        AdminStudent student = new AdminStudent();
        student.setGroupName(groupName);
        student.setStuName(stuName);
        student.setStuNumber(stuNumber);

        //查询
        List<AdminStudent> list = adminGroupDao.queryStudentByGroupIdCombination(student,groupId);
        int total = list.size();

        if (null == list || total == 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success", 0, 0, total, list);


    }


    /**
     * 更换学生分组 支持批量更新
     * @param groupId 分组Id
     * @param ListStuId 学生ID
     * @return
     */
    @Override
    public Page updateStudentGroupById(Integer groupId, List ListStuId) {
        System.out.println("impl测试ListStuId:"+ListStuId);
        int msg = 0;
        for (int i= 0;i<ListStuId.size();i++){
            int stuId = (int) ListStuId.get(i);
            System.out.println("impl测试学生ID："+ListStuId.get(i));
             msg = adminGroupDao.updateStudentGroupById(groupId, stuId);
            msg +=1;
        }
        if (msg>0){
            return Page.SUCCESS;
        }else{
            return Page.FAIL;
        }
    }


    /**
     * 查询所有未结业的学生
     * @param collegeName 学员名
     * @param majorName 专业
     * @param gradeName 年级
     * @param className 班级
     * @param stuName 学生姓名
     * @param stuNumber 学号
     * @return
     */
    @Override
    public Page<AdminStudent> qureyAllStudentNoGraduation(String collegeName, String majorName, String gradeName, String className, String stuName, String stuNumber) {

        AdminStudent student = new AdminStudent();
        student.setCollegeName(collegeName);
        student.setMajorName(majorName);
        student.setGradeName(gradeName);
        student.setClassName(className);
        student.setStuName(stuName);
        student.setStuNumber(stuNumber);

        List<AdminStudent> list = adminGroupDao.qureyAllStudentNoGraduation(student);
        int total = list.size();
        if (null == list || total == 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success", 0, 0, total, list);
    }


    /**
     * 小组新增学生
     * @param groupId 小组ID
     * @param ListStuId　学生ID
     * @return
     */
    @Override
    public Page addStudentToGroup(Integer groupId, List ListStuId) {
        int msg = 0;
        for (int i= 0;i<ListStuId.size();i++){
            int stuId = (int) ListStuId.get(i);
            msg = adminGroupDao.addStudentToGroup(groupId, stuId);
            msg +=1;
        }
        if (msg>0){
            return Page.SUCCESS;
        }else{
            return Page.FAIL;
        }
    }


    /**
     * 删除小组某个学生 支持批量操作
     * @param ListStuId 学生ID
     * @return
     */
    @Override
    public Page deleteStudentByGroupById(List ListStuId) {
        int msg = 0;
        for (int i= 0;i<ListStuId.size();i++){
            int stuId = (int) ListStuId.get(i);
            msg = adminGroupDao.deleteStudentByGroupById(stuId);
            msg +=1;
        }
        if (msg>0){
            return Page.SUCCESS;
        }else{
            return Page.FAIL;
        }
    }


}
